import Vue from "vue"
import VueRouter from "vue-router"
Vue.use(VueRouter);

// 懒加载导入组件
const Login = () => import("@/pages/Login");
const Home = () => import("@/pages/Home");
const Search = () => import("@/pages/Search");
const Register = () => import("@/pages/Register");
const Test = () => import("@/pages/Test");


//增强push方法
//1. 备份 VueRouter.prototype.push  方法
/* const originPush = VueRouter.prototype.push;
// 2. 增强 push 方法
VueRouter.prototype.push = function(location,okCallback,errorCallback){
    if(okCallback || errorCallback){
        originPush.call(this,location,okCallback,errorCallback);
    }else{
        originPush.call(this,location).catch(()=>{});
    }
} */

/* //增强replace方法
//1. 备份 VueRouter.prototype.push  方法
const originReplace = VueRouter.prototype.replace;
// 2. 增强 push 方法
VueRouter.prototype.replace = function(location,okCallback,errorCallback){
    if(okCallback || errorCallback){
        originReplace.call(this,location,okCallback,errorCallback);
    }else{
        originReplace.call(this,location).catch(()=>{});
    }
} */


// 创建一个增强的方法
function enhance(source,type){
    //1. 备份 source.prototype[type]  方法
    const origin = source.prototype[type];
    // 2. 增强 push 方法
    source.prototype[type] = function(location,okCallback,errorCallback){
        if(okCallback || errorCallback){
            origin.call(this,location,okCallback,errorCallback);
        }else{
            origin.call(this,location).catch(()=>{});
        }
    }
}

//增强 VueRouter 的 push 方法
enhance(VueRouter,"push");
//增强 VueRouter 的 replace 方法
enhance(VueRouter,"replace");


export default new VueRouter({
    mode:"history",
    routes:[
        {
            path:"/",
            component:Home
        },
        {
            path:"/test",
            component:Test
        },
        {
            path:"/login",
            component:Login,
            meta:{
                isHiddenFooter:true,  
            }
        },
        {
            path:"/search",
            component:Search
        },
        {
            path:"/register",
            component:Register,
            meta:{
                isHiddenFooter:true,  
            }
        }
    ]
})